package com.oocl.hgl.crm.service.impl;

import java.util.Scanner;

import com.oocl.hgl.crm.entity.Person;
import com.oocl.hgl.crm.service.PersionService;
import com.oocl.hgl.crm.service.Service;
import com.oocl.hgl.crm.store.PersonInfoStorer;
import com.oocl.hgl.crm.util.AppHelper;
import com.oocl.hgl.crm.util.Checker;
import com.oocl.hgl.crm.util.DoubleLinkedList;

public class BasicAppServiceImpl  implements Service{
	
	private PersionService pService =  new PersonServiceImpl();
	public BasicAppServiceImpl() {
		super();
	}

	public boolean doService(String inputStr){
		if( inputStr.startsWith("L") ){
			query(inputStr);
		}else if( inputStr.startsWith("A") ){
			add(inputStr);
		}else if( inputStr.startsWith("D") ){
			delete(inputStr);
		}else if( inputStr.startsWith("U") ){
			update(inputStr);
		}else if( inputStr.startsWith("O") ){
			order(inputStr);
		}else if( inputStr.startsWith("H") ){
			help(inputStr);
		}else if( inputStr.startsWith("Q") ){
			return quit(inputStr);
		}else{
			pritHelpDoc();
		}
		return WORK;
	}
	
	private void showMenu() {
		AppHelper.menu();
	}
	
	public static final boolean QUIT = false;
	public static final boolean WORK = true;
	
	public void work() {
		showMenu();
		Scanner sc = new Scanner(System.in);
		boolean isRun = true;
		do{
			String inputStr = sc.nextLine();
			isRun = doService(inputStr);
		}while(isRun);
	}

	
	private void pritHelpDoc() {
		AppHelper.help();
	}
	/**
	 * 前置条件 inputStr.startsWith("O") , O id | O name
	 * @param inputStr
	 */
	private void order(String inputStr) {
		String[] strArr = inputStr.split(" ");
		if(strArr!=null && strArr.length==2){
			if( "id".equals(strArr[1]) ){
				DoubleLinkedList<Person> tmpList = pService.sortById();
				tmpList.printList();
			}
			if( "name".equals(strArr[1]) ){
				DoubleLinkedList<Person> tmpList = pService.sortByName();
				tmpList.printList();
			}
		}
	}
	/**
	 * 前置条件，inputStr startWith 'H' , H X （其中 X 可以为 L、A、D、U、O）
	 * @param inputStr 
	 */
	private void help(String inputStr) {
		if(inputStr.length()==1){
			AppHelper.menu();
		}
		String[] eleArr = inputStr.split(" ");
		if(eleArr!=null && eleArr.length==2){
			AppHelper.printHelpDoc(eleArr[1]);
		}
	}

	/**
	 * 前置条件 inputStr.startsWith("U") 
	 * U id name:xxx[,gender:xxx][,birthday:xxx][,address:xxx][,phone:xxx]
	 * @param inputStr
	 */
	private void update(String inputStr) {
		String[] eleArr = inputStr.split(" ");
		int id = -1;
		if(eleArr!=null && eleArr.length==3){
			id = Integer.parseInt(eleArr[1]);
		}
		Person p = pService.get( id );
		if(p!=null){
			//
			setPersonAttr(p, eleArr[2]);
			//
			System.out.println( "update suc,with id = " + p.getId() +",it's info:");
			System.out.println( p );
		}else{
			System.out.println( "update fail,bcs person with id = " + id +" is not found");
		}
	}

	private void setPersonAttr(Person p, String inputStr) {
		String[] supperArr = inputStr.split(",");
		for (String s : supperArr) {
			String[] strArr = s.split(":");
			if(strArr!=null && strArr.length==2){
				if( "name".equals(strArr[0]) ){
					p.setName(strArr[1]);
				}
				if( "gender".equals(strArr[0]) ){
					if(PersionService.MALE_VIEW.equals(strArr[1])){
						p.setGender( PersionService.MALE_VALUE );
					}
					if(PersionService.FEMALE_VIEW.equals(strArr[1])){
						p.setGender( PersionService.FEMALE_VALUE );
					}
				}
				if( "birthday".equals(strArr[0]) ){
					p.setBirthday( AppHelper.getBirthday(strArr[1],Person.DATE_FORMAT) );
				}
				if( "address".equals(strArr[0]) ){
					p.setAddress(strArr[1]);
				}
				if( "phone".equals(strArr[0]) && Checker.isDecimal(strArr[1]) ){
					p.setPhone(strArr[1]);
				}
			}
		}
	}

	/**
	 * 前置条件 inputStr.startsWith("D") 
	 * D id
	 * @param inputStr
	 */
	private void delete(String inputStr) {
		// 
		inputStr = inputStr.substring(inputStr.indexOf("D")+1).trim();
		Person p;
		if(Checker.isDecimal(inputStr)){
			//
			p = pService.get( Integer.parseInt(inputStr) );
			if(p!=null){
				pService.delete(p.getId()); 
				System.out.println( "delete suc,with id = " + p.getId() +",it's info:");
				System.out.println( p );
				return;
			}else{
				System.out.println( "delete fail,bcs person with id = " + Integer.parseInt(inputStr) +" is not found");
			}
		}
	}

	/**
	 * 前置条件 inputStr.startsWith("A") 
	 * A name:xxx[,gender:xxx][,birthday:xxx][,address:xxx][,phone:xxx]
	 * @param inputStr
	 */
	private void add(String inputStr) {
		inputStr = inputStr.substring(inputStr.indexOf("A")+1).trim();
		if(inputStr.indexOf("name")>=0){
			//
			Person p = new Person();
			setPersonAttr(p, inputStr);
			//
			if(pService.add(p)!=null){
				System.out.println( "add succ,with id = " + p.getId() + " , it's info:");
			}
		}else{
			System.out.println( "add fail,bcs name is required ");
		}
	}

	/**
	 * 前置条件 inputStr.startsWith("L") 
	 * @param inputStr
	 */
	private void query(String inputStr) {
		//
		if(inputStr.length()==1){
			DoubleLinkedList<Person> tmpList = PersonInfoStorer.pList;
			if(tmpList!=null && tmpList.size()>0){
				for(int i=0;i<tmpList.size();i++){
					System.out.println( tmpList.get(i).getView() );
				}
			}
			return;
		}
		//
		inputStr = inputStr.substring(inputStr.indexOf("L")+1).trim();
		if(inputStr.startsWith("name")){
			String[] strArr = inputStr.split(":");
			if(strArr!=null && strArr.length==2){
				DoubleLinkedList<Person> tmpList = pService.queryByName(strArr[1]);
				if(tmpList!=null && tmpList.size()>0){
					for(int i=0;i<tmpList.size();i++){
						System.out.println( tmpList.get(i).getView() );
					}
				}
			}
		}
	}

	
	private boolean quit(String inputStr) {
		if(inputStr!=null && inputStr.length()==1){
			System.out.println("程序退出");
			return QUIT;
		}
		return WORK;
	}

	
}
